package com.admin.control;

import com.grid.entity.AQI;
import com.admin.entity.TableData;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableView;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;

public class AQItrendtotalstatisViewControl {
    @FXML
    private TableView<TableData> tableView;
    private static final String AQI_FILE = "AQI.txt";
    private List<AQI> aqilist = new ArrayList<>();

    private int count1 = 0;
    private int count2 = 0;
    private int count3 = 0;
    private int count4 = 0;
    private int count5 = 0;
    private int count6 = 0;
    private int count7 = 0;
    private int count8 = 0;
    private int count9 = 0;
    private int count10 = 0;
    private int count11 = 0;
    private int count12 = 0;

    private List<AQI> loadAQIData() throws IOException {
        File file = new File(AQI_FILE);
        if (!file.exists() || file.length() == 0) {
            return new ArrayList<>();
        }

        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
            return (List<AQI>) ois.readObject();
        } catch (ClassNotFoundException e) {
            throw new IOException("数据文件格式错误", e);
        }
    }

    @FXML
    private void initialize() {
        try {
            aqilist = loadAQIData();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        for (AQI month : aqilist) {
            if (month.getMonth() == 6 && month.getAQI() >= 101) {
                count1++;
            } else if (month.getMonth() == 5 && month.getAQI() >= 101) {
                count2++;
            } else if (month.getMonth() == 4 && month.getAQI() >= 101) {
                count3++;
            } else if (month.getMonth() == 3 && month.getAQI() >= 101) {
                count4++;
            } else if (month.getMonth() == 2 && month.getAQI() >= 101) {
                count5++;
            } else if (month.getMonth() == 1 && month.getAQI() >= 101) {
                count6++;
            } else if (month.getMonth() == 12 && month.getAQI() >= 101) {
                count7++;
            } else if (month.getMonth() == 11 && month.getAQI() >= 101) {
                count8++;
            } else if (month.getMonth() == 10 && month.getAQI() >= 101) {
                count9++;
            } else if (month.getMonth() == 9 && month.getAQI() >= 101) {
                count10++;
            } else if (month.getMonth() == 8 && month.getAQI() >= 101) {
                count11++;
            } else if (month.getMonth() == 7 && month.getAQI() >= 101) {
                count12++;
            }
        }

        ObservableList<TableData> data = FXCollections.observableArrayList(
                new TableData("1", "2025-05", String.valueOf(count1)),
                new TableData("2", "2025-04", String.valueOf(count2)),
                new TableData("3", "2025-03", String.valueOf(count3)),
                new TableData("4", "2025-02", String.valueOf(count4)),
                new TableData("5", "2025-01", String.valueOf(count5)),
                new TableData("6", "2024-12", String.valueOf(count6)),
                new TableData("7", "2024-11", String.valueOf(count7)),
                new TableData("8", "2024-10", String.valueOf(count8)),
                new TableData("9", "2024-09", String.valueOf(count9)),
                new TableData("10", "2024-08", String.valueOf(count10)),
                new TableData("11", "2024-07", String.valueOf(count11)),
                new TableData("12", "2024-06", String.valueOf(count12))
        );
        tableView.setItems(data);
    }
}
